package grammarbot

import twitter4j.StatusListener
import static TwitterUtils.*

def twitter = TwitterUtils.getInstance()

// Define type - random, attweets, or followers
def type = System.getProperty("type")
if(!type) {
  type = "followers"
}

// If debug is true tweetHandlers won't post responses.
def debug = false
def debugStr = System.getProperty("debug")
if(debugStr) {
  debug = Boolean.parseBoolean(debugStr)
}


def tweetChecker = new TweetChecker()
def tweetHandler = new TweetHandler(tweetChecker : tweetChecker, twitter : twitter, debug : debug)

def twitterStream = TwitterUtils.getStreamInstance()
twitterStream.statusListener = [
  onDeletionNotice : { note -> return },
  onException : { ex -> println ex},
  onTrackLimitationNotice : { numberOfLimitedStatuses -> println("onTrackLimitationNotice => ${numberOfLimitedStatuses}") },
  onStatus : { status -> tweetHandler.handleStatus(status) }
] as StatusListener

println("Handling stream of type ${type} as user ${twitter.screenName}. Debug mode? ${debug}")

switch(type) {
  case "followers": // Check followers grammars.
    twitterStream.filter(filterQueryFollowers(twitter))
    break
  case "attweets": // Check grammar of tweets @'ed the bot.
    tweetHandler.responseToCorrect = "Grammar good."
    twitterStream.filter(filterQueryAtTweetingAccount(twitter))
    break
  case "random": // Check random tweets.
    twitterStream.sample()
    break
}

// Sleep indefinitely
while(true) {
  Thread.sleep 10000
}